import os
import sys
from pathlib import Path

import sass


def compile_scss(src_file: Path, dst_file: Path):
    print(f'compile scss [{src_file}] => [{dst_file}]')
    if not dst_file.parent.exists():
        os.makedirs(dst_file.parent)
    with open(src_file, mode='r', encoding='utf-8') as srcfile:
        with open(dst_file, mode='w', encoding='utf-8') as dstfile:
            dstfile.write(sass.compile(
                string=srcfile.read(),
                include_paths=[str(src_file.parent.absolute())]
            ))


def build_css(web_root: Path):
    src = web_root / 'style'
    dst = web_root / 'build'
    compile_scss(src / 'book.scss', dst / 'book.css')
    compile_scss(src / 'topbar.scss', dst / 'topbar.css')
    compile_scss(src / 'main.scss', dst / 'main.css')


def main():
    if len(sys.argv) < 1 + 1:
        print(f'Usage: {Path(sys.argv[0]).name} <path-of-web-resource-root>')
        sys.exit(1)
    web_root: Path = Path(sys.argv[1]).absolute()
    print(f'web root: [{web_root}]')
    build_css(web_root)


if __name__ == '__main__':
    main()
